package com.sgb.kjwl.view.ui.fragment.pick;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickCategoryBinding;
import com.sgb.kjwl.view.ui.activity.pick.PickListActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickCategoryLeftAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickCategoryRightAdapter;
import com.sgb.kjwl.viewmodel.pick.PickCategoryFragmentModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.List;


/****
 * 分类界面
 */
public class PickCategoryFragment extends AppLazyFragment {

    public PickCategoryFragmentModel viewModel;
    private FragmentPickCategoryBinding binding;
    private PickCategoryLeftAdapter leftAdapter;
    private PickCategoryRightAdapter rightAdapter;
    private GridLayoutManager leftManager;
    private GridLayoutManager rightManager;
    private int leftPosition;
    private String searchName;
    private String[][] titles = new String[][]{
            new String[]{"机械分类", "机械类型"},
            new String[]{"工程类别", "专业分类", "业务类别"},
            new String[]{"商品分类", "商品类别", "商品品名"},
            new String[]{"工程类别", "专业分类", "业务类型"},
            new String[]{"设备分类", "设备类型"},
            new String[]{"房产类型", "房产分类"},
            new String[]{"货物类型", "要求车型"},
    };

    @Override
    protected void onLazyLoad() {
        viewModel.getFilterTypeData();
    }

    public static PickCategoryFragment newInstance() {
        PickCategoryFragment fragment = new PickCategoryFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_category, container, false);
        viewModel = new ViewModelProvider(this).get(PickCategoryFragmentModel.class);
        viewModel.setActivity(PickCategoryFragment.this);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    public void setTenderGoodsCategoryAllList(List<CommonPopEntity> data) {
        if (leftPosition == 19) {
            data.add(data.size(), new CommonPopEntity("", "其他"));
        } else {
            if ("全部".equals(data.get(0).getTypeName())) {
                data.remove(0);
            }
        }
        rightAdapter.setDatas(data);
        binding.rightRecycler.scrollToPosition(0);
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        if ("全部".equals(datas.get(0).getPickerViewText())) {
            datas.remove(0);
        }
        showLeft(leftPosition,datas.get(leftPosition));
        leftAdapter.setDatas(datas);
        leftAdapter.setDefItem(leftPosition);
    }

    public void showTitle(int category) {
        leftAdapter.setDefItem(leftPosition);
        if (category == 6 || category == 7 || category == 8 || category == 9 || category == 10) {
            binding.tvTypeTitle.setText(titles[3][0]);
        } else if (category == 11 || category == 15) {
            binding.tvTypeTitle.setText(titles[5][0]);
        } else if (category == 12 || category == 18) {
            binding.tvTypeTitle.setText("车型");
        } else if (category == 13) {
            binding.tvTypeTitle.setText("工程类别");
        } else if (category == 14) {
            binding.tvTypeTitle.setText("土地用途");
        } else if (category == 16) {
            binding.tvTypeTitle.setText(titles[0][0]);
        } else if (category == 17) {
            binding.tvTypeTitle.setText(titles[4][0]);
        } else if (category == 19) {
            binding.tvTypeTitle.setText(titles[6][0]);
        } else {
            if (category > 0 && category != 20) {
                binding.tvTypeTitle.setText(titles[category - 1][0]);
            } else if (category == 20) {
                binding.tvTypeTitle.setText("");
            }
        }

        if (TextUtils.isEmpty(binding.tvTypeTitle.getText().toString())) {
            binding.tvTypeTitle.setVisibility(View.GONE);
        } else {
            binding.tvTypeTitle.setVisibility(View.VISIBLE);
        }
    }

    public void initView() {
        binding.includeSearch.etView.setHint("搜索招标名称、采购单位");
        binding.includeSearch.etView.setTextColor(getContext().getResources().getColor(R.color.color_tab_one));
        binding.includeSearch.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.includeSearch.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (TextUtils.isEmpty(binding.includeSearch.etView.getText().toString())) {
                    MToast.showToast(getContext(), binding.includeSearch.etView.getHint().toString());
                    return true;
                }
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    searchName = binding.includeSearch.etView.getText().toString();
                    PickListActivity.start(getContext(), "1", searchName);
                }
                return false;
            }
        });

        binding.includeSearch.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    binding.includeSearch.tvSearchDelete.setVisibility(View.GONE);
                } else {
                    binding.includeSearch.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        binding.includeSearch.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.includeSearch.etView.setText("");
                searchName = "";
            }
        });

        binding.includeSearch.searchText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(binding.includeSearch.etView.getText().toString())) {
                    MToast.showLongToast1(getContext(), "请输入搜索内容", Gravity.CENTER);
                } else {
                    searchName = binding.includeSearch.etView.getText().toString();
                    PickListActivity.start(getContext(), "1", searchName);
                }
            }
        });

        if (leftAdapter == null) {
            leftAdapter = new PickCategoryLeftAdapter(getActivity(), null);
        }
        leftManager = new GridLayoutManager(getActivity(), 1);
        binding.leftRecycler.setLayoutManager(leftManager);
        binding.leftRecycler.setAdapter(leftAdapter);
        leftAdapter.setItemSelect(true);
        leftAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                showLeft(position,pickSelectEntity);
            }
        });

        if (rightAdapter == null) {
            rightAdapter = new PickCategoryRightAdapter(getActivity(), null);
        }
        rightManager = new GridLayoutManager(getActivity(), 3);
        binding.rightRecycler.setLayoutManager(rightManager);
        binding.rightRecycler.setAdapter(rightAdapter);
        binding.rightRecycler.setLoadingMoreEnabled(false);
        binding.rightRecycler.setPullRefreshEnabled(false);
        rightAdapter.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity commonPopEntity, int position) {
                PickListActivity.start(getContext(), "1", leftPosition + 1, position + 1);
            }
        });

    }

    private void showLeft(int position,PickSelectEntity pickSelectEntity){
        viewModel.getTenderGoodsCategoryAllList(pickSelectEntity.getNo());
        leftPosition = position;
        showTitle(Integer.parseInt(pickSelectEntity.getNo()));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}